package com.wzcl.app.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wzcl.app.service.common.MyPLChannelService;
import com.wzcl.app.service.common.model.requset.PlChannelAuthGetRequest;
import com.wzcl.app.service.common.model.requset.PlChannelDetailUpdateRequest;
import com.wzcl.app.service.common.model.requset.PlChannelEndRequest;
import com.wzcl.app.service.common.model.result.PLChannelAuthResult;
import net.polyv.live.bean.client.WrappedResponse;
import net.polyv.live.bean.result.channel.PLChannelCommonResult;
import net.polyv.live.service.PLAbstractService;

import java.util.ArrayList;
import java.util.List;

public class MyPLChannelServiceImpl extends PLAbstractService implements MyPLChannelService {

    public MyPLChannelServiceImpl() {
    }

    public List<PLChannelAuthResult> getAuth(PlChannelAuthGetRequest request) {
        WrappedResponse response = this.request("http://api.polyv.net/live/v3/channel/auth/get", request.getParams(), "GET");
        if (response.isRequestOk()) {
            JSONArray array = JSONObject.parseArray(JSON.toJSONString(response.getData()));
            if(array.size()>0){
                List<PLChannelAuthResult> result = new ArrayList<>();
                for (int i = 0; i < array.size(); i++) {
                    result.add(array.getObject(i, PLChannelAuthResult.class));
                }
                return result;
            }
        }
        return null;
    }

    public PLChannelCommonResult updateDetail(PlChannelDetailUpdateRequest request) {
        WrappedResponse response = this.request("http://api.polyv.net/live/v3/channel/detail/update", request.getParams(), "POST");
        PLChannelCommonResult result = new PLChannelCommonResult();
        if (response.isRequestOk()) {
            result.setData(response.getData());
        }
        return this.getResult(response, result);
    }

    public PLChannelCommonResult end(Integer channelId,PlChannelEndRequest request) {
        String url = String.format("http://api.polyv.net/live/v2/channels/%d/end", channelId);
        WrappedResponse response = this.request(url, request.getParams(), "POST");
        PLChannelCommonResult result = new PLChannelCommonResult();
        if (response.isRequestOk()) {
            result.setData(response.getData());
        }
        return this.getResult(response, result);
    }


}
